local skynet = require "skynet"
local MessageType = require "common.message_type"

local player_listener = {}

local listeners = {}

function player_listener.addListener(listener)
    for _,v in ipairs(listeners) do
        if listener == v then
            return false
        end
    end
    table.insert(listeners,listener)
    return true
end

local function notifyAllListeners(msgId,...)
    for _,v in ipairs(listeners) do
        pcall(skynet.send,v,"lua",msgId,...)
    end
end

function player_listener.onPlayerOnline(player)
    notifyAllListeners(MessageType.OnPlayerOnline,player:getOpenID())
end

function player_listener.onPlayerOffline(player)
    notifyAllListeners(MessageType.OnPlayerOffline,player:getOpenID())
end

return player_listener